package com.he.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.he.baseservice.exceptionhandler.EduException;
import com.he.eduservice.entity.EduCourse;
import com.he.eduservice.entity.EduCourseDescription;
import com.he.eduservice.entity.course.CourseInfoForm;
import com.he.eduservice.entity.course.CoursePublishVo;
import com.he.eduservice.mapper.EduCourseMapper;
import com.he.eduservice.service.EduCourseDescriptionService;
import com.he.eduservice.service.EduCourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author testhejinyu
 * @since 2021-06-02
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {

    @Autowired
    private EduCourseDescriptionService courseDescriptionService;

    @Override
    public String addCourse(CourseInfoForm data) {
        //添加课程基本信息
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(data, eduCourse);
        int courseInsert = baseMapper.insert(eduCourse);
        if (courseInsert <= 0) {
            throw new EduException(2001, "添加课程失败");
        }
        String courseId = eduCourse.getId();
        //添加课程简介信息
        EduCourseDescription courseDescription = new EduCourseDescription();
        courseDescription.setId(courseId);
        courseDescription.setDescription(data.getDescription());
        courseDescriptionService.save(courseDescription);
        return courseId;
    }

    @Override
    public CourseInfoForm findCourseInfoByCourseId(String courseId) {
        EduCourse eduCourse = baseMapper.selectById(courseId);
        CourseInfoForm courseInfoForm = new CourseInfoForm();
        //将查的内容直接赋值到 返回实体中
        BeanUtils.copyProperties(eduCourse, courseInfoForm);

        EduCourseDescription courseDescription = courseDescriptionService.getById(courseId);
        courseInfoForm.setDescription(courseDescription.getDescription());

        return courseInfoForm;
    }

    @Override
    public void updateCourseInfoById(CourseInfoForm data) {
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(data, eduCourse);
        int index = baseMapper.updateById(eduCourse);
        if (index <= 0) {
            throw new EduException(2001, "修改课程失败");
        }

        EduCourseDescription courseDescription = new EduCourseDescription();
        courseDescription.setDescription(data.getDescription());
        courseDescription.setId(data.getId());
        boolean flag = courseDescriptionService.updateById(courseDescription);
        if (!flag) {
            throw new EduException(2001, "修改课程详细信息失败");
        }
    }

    @Override
    public CoursePublishVo selectCoursePublishVoById(String id) {
        CoursePublishVo coursePublishVo = baseMapper.selectCoursePublishVoById(id);
        return coursePublishVo;
    }

    @Override
    public boolean publishCourseById(String id) {
        EduCourse eduCourse = new EduCourse();
        eduCourse.setId(id);
        eduCourse.setStatus("Normal");
        int update = baseMapper.updateById(eduCourse);
        return update > 0;
    }

    @Cacheable(value = "courseList",key = "'selectCourseList'")
    @Override
    public List<EduCourse> findCourseList() {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.orderByDesc("view_count");
        wrapper.last("limit 8");
        List<EduCourse> list = baseMapper.selectList(wrapper);
        return list;

    }


}
